// ==UserScript==
// @icon			
// @name			Feliratok.info addon
// @namespace		http://feliratok-info-gm-script.googlecode.com/files/feliratokinfo.latest.user.js
// @description		Gyűtsd össze a kedvenc felirataidat, hogy gyorsabban elérhesd őket.
// @version			0.1
//
// @include			http://www.feliratok.info/*
//
//			Készítette:
//				Roncsák Tamás
//
// ==/UserScript==

if ( navigator.userAgent.indexOf("Chrome") != -1 ) {
	addJQuery = function(callback) {
		var script = document.createElement("script");
		script.setAttribute("src", "http://ajax.googleapis.com/ajax/libs/jquery/1.5/jquery.min.js");
		script.addEventListener('load', function() {
			var script = document.createElement("script");
			script.textContent = "(" + callback.toString() + ")();";
			document.body.appendChild(script);
		}, false);
		document.body.appendChild(script);
	}
	addJQuery(main);
} else if ( navigator.userAgent.indexOf("Firefox") != -1 ) {
	var i = 0;
	function GM_wait() {
		i++;
		//console.log(unsafeWindow);
		if( typeof unsafeWindow.jQuery == 'undefined' || typeof unsafeWindow.jQuery.ui == 'undefined' ) {
			if( i == 3 ) { // after 0,3 sec load jQuery any way
				if( typeof unsafeWindow.jQuery == 'undefined' ) {
					var GM_JQ = document.createElement('script');
					GM_JQ.src = 'http://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js';
					GM_JQ.type = 'text/javascript';
					document.getElementsByTagName('head')[0].appendChild(GM_JQ);
				}
				if( typeof unsafeWindow.jQuery.ui == 'undefined' ) {
					var GM_JQUI = document.createElement('script');
					GM_JQUI.src = 'https://ajax.googleapis.com/ajax/libs/jqueryui/1/jquery-ui.min.js';
					GM_JQUI.type = 'text/javascript';
					document.getElementsByTagName('head')[0].appendChild(GM_JQUI);
				}
			}
			window.setTimeout(GM_wait,100);
		} else {
			$ = unsafeWindow.jQuery;
			var css = "\
#gm_favs {\
	/*height: 42px;*/\
	padding: 2px\
}\
#dropHere_container {\
	display: none;\
	line-height: 42px;\
	margin-left: 4px;\
	position: relative;\
	top: -14px;\
}\
#dropHere {\
	width: 120px;\
	height: 38px;\
	text-align: center;\
	border: 2px dashed #000000;\
	line-height:42px;\
}\
#dropHere_container.drophover {\
	background-color: #BFFFBF;\
}\
.ui-autocomplete {\
    cursor: default;\
    position: absolute;\
}\
.ui-state-hover, .ui-widget-content .ui-state-hover, .ui-state-focus, .ui-widget-content .ui-state-focus {\
    background-color: FDF5CE;\
    border: 1px solid #FBCB09;\
    color: #C77405;\
    font-weight: bold;\
    outline: medium none;\
}\
.ui-menu .ui-menu-item a {\
    display: block;\
    line-height: 1.5;\
    padding: 0.2em 0.4em;\
    text-decoration: none;\
}\
.ui-menu .ui-menu-item a.ui-state-hover, .ui-menu .ui-menu-item a.ui-state-active {\
    font-weight: normal;\
    margin: -1px;\
}\
.ui-state-hover a, .ui-state-hover a:hover {\
    color: #C77405;\
    outline: medium none;\
    text-decoration: none;\
}\
.ui-menu {\
	display: block;\
	float: left;\
	list-style: none outside none;\
	margin: 0;\
	padding: 2px;\
}\
.ui-widget {\
	font-family: Trebuchet MS,Tahoma,Verdana,Arial,sans-serif;\
	font-size: 1.1em;\
}\
.ui-widget *:active {\
	outline: medium none;\
}\
.ui-widget-content {\
	background-color: #EEEEEE;\
	border: 1px solid #DDDDDD;\
	color: #333333;\
}\
.ui-corner-all {\
	border-radius: 4px;\
}\
/"; 
			GM_addStyle( css );
			main();
		}
		/*var GM_JQUI = document.createElement('script');
		GM_JQUI.src = 'https://ajax.googleapis.com/ajax/libs/jqueryui/1.8.16/jquery-ui.min.js';
		GM_JQUI.type = 'text/javascript';
		document.getElementsByTagName('head')[0].appendChild(GM_JQUI);*/
	}
	GM_wait();
}
function sortJson(a,b) {
	if( a.title > b.title) {
        return 1;
	} else if( a.title == b.title ) {
		return 0;
    } else {
        return -1;
    }
}
function main() {
	function gm_save( opt ) {
		var ertek=JSON.stringify(opt);
		setTimeout(function(){GM_setValue('set1', ertek);}, 0);
	}
	function gm_load() {
		var opt={};
		var arr = GM_getValue('set1', 'na');
		if( arr == 'na' ) {
			opt = {set:'empty'};
		} else {
			//$.extend(opt, JSON.parse(arr));
			opt = $.parseJSON(arr);
		}
		return opt;
	}
	function buildFavs( series ) {
		$('#gm_favs a').remove();
		var source = [];
		$.each(series, function(i,l) {
			if( series[i] != undefined ) {
				$('#dropHere_container').before( $( '<a id="gm_s'+l.id+'" href="index.php?sid='+l.id+'" class="fav"></a>' ).html( ' <img src="sorozat_cat.php?kep='+l.id+'" class="kategk" title="'+l.title+'">' ) );
				source.push(l.title);
			}
		});
		$('#gm_favs a.fav').draggable({
			  helper: 'clone'
			, opacity: 0.35
			, start: function(event, ui) {
				$('#dropHere_container').css('display','inline-block');
			}
			, snap: '#dropHere'
			, snapMode: 'inner'
		});
		$( ".searchInput" ).autocomplete({
			source: source
		});
	}
	$('.menuDivMain').after('<div id="gm_favs"></div>');
	$('<div id="dropHere_container"><div id="dropHere">Hozzáad</div></div>').appendTo( '#gm_favs' );
	var series = gm_load();
	if( !series.set ) {
		buildFavs( series );
	}
	$('#dropHere_container').droppable({
		  accept: '.ui-draggable'
		, activate: function( event, ui ) {
			// $('#dropHere_container').css('display','inline-block');
			if( series.set ) {
				$(this).css('top','0px');
			} else {
				$(this).css('top','-14px');
			}
			if( ui.draggable.hasClass('fav') ) {
				$('#dropHere').text('Töröl');
			} else {
				$('#dropHere').text('Hozzáad');
			}
		  }
		, deactivate: function( event, ui ) {
			$(this).hide('fast');
		  }
		, drop: function( event, ui ) {
			//$(ui).clone().appendTo('#gm_favs');
			if( ui.draggable.hasClass('fav') ) {
				var item = ui.draggable.attr('href').substr(ui.draggable.attr('href').indexOf("=")+1);
				$.each(series, function(i, l) {
					if(l.id == item) {
						delete series[i];
						(series.sort(sortJson)).pop();
						$('#gm_s'+l.id).remove();
						//console.log('gotcha');
						gm_save( series );
						buildFavs( series );
						return false;
					}
				});
			} else {
				if( series.set ) {
					delete series.set;
					series = [];
				}
				var item = ui.draggable.attr('href').substr(ui.draggable.attr('href').indexOf("=")+1);
				var title = ui.draggable.parent().siblings('.gm_title').children('.eredeti').text();
				// var pattern = /.+(?= - \d+x\d+)/ig;
				var pattern = /.+(?= \(Season| - \d+x\d+)/ig;
				//console.log('add');
				series.push({"id":item,"title":title.match(pattern)[0]});
				gm_save( (series).sort(sortJson) );
				buildFavs( series );
			}
		  }
		, hoverClass: 'drophover'
		/*, over: function(event, ui) {
			$(this).css('background','#FF0000');
		}*/
	});
	$('.searchInput').after('<select id="gm_episode" name="gm_episode" ><option value="">--</option></select>');
	$('.searchInput').after('<select id="gm_season" name="gm_season" ><option value="">--</option></select>');
	var num = 0;
	while( num <= 30 ) {
		if( num < 10 ) {
			$('#gm_episode').append('<option value="0'+num+'">0'+num+'</option>');
		} else {
			$('#gm_episode').append('<option value="'+num+'">'+num+'</option>');
		}
		if( num > 0 && num < 16 ) $('#gm_season').append('<option value="'+num+'">'+num+'</option>');
		num++;
	}
	var loc = window.location.href;
	var pattern = /gm_\w+=([0-9]+)/igm;
	if( loc.match(pattern) ) {
		$.each( loc.match(pattern), function(i,l) {
			if( l.indexOf('_season') != -1 ) {
				$('#gm_season').val( l.substr(10) );
			} else if( l.indexOf('gm_episode') != -1 ) {
				$('#gm_episode').val( l.substr(11) );
			}
		});
	}
	pattern = /.+(?= \(Season| - \d+x\d+)/ig;
	if( $('.searchInput').val().match(pattern) ) {
		$('.searchInput').val($('.searchInput').val().match(pattern)[0]);
	}
	$('form[name="searchF"]').submit(function() {
		if( $('#gm_season').val() != "" && $('#gm_episode').val() != "" ) {
			$('.searchInput').val( $.trim($('.searchInput').val()) + ' - '+$('#gm_season').val()+'x'+ $('#gm_episode').val() );
		}
		return true;
	});
	$('.resultTableDiv .magyar').parent().addClass('gm_title');
	$('.resultTableDiv .kategk').parent().draggable({
		  helper: 'clone'
		, opacity: 0.35
		, start: function(event, ui) {
			$('#dropHere_container').css('display','inline-block');
		  }
		, snap: '#dropHere'
		, snapMode: 'inner'
	});
}